package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.TRechargeVo;
import org.dromara.system.domain.bo.TRechargeBo;
import org.dromara.system.service.ITRechargeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 充值码定义
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/recharge")
public class TRechargeController extends BaseController {

    private final ITRechargeService tRechargeService;

    /**
     * 查询充值码定义列表
     */
    @SaCheckPermission("system:recharge:list")
    @GetMapping("/list")
    public TableDataInfo<TRechargeVo> list(TRechargeBo bo, PageQuery pageQuery) {
        return tRechargeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出充值码定义列表
     */
    @SaCheckPermission("system:recharge:export")
    @Log(title = "充值码定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TRechargeBo bo, HttpServletResponse response) {
        List<TRechargeVo> list = tRechargeService.queryList(bo);
        ExcelUtil.exportExcel(list, "充值码定义", TRechargeVo.class, response);
    }

    /**
     * 获取充值码定义详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:recharge:query")
    @GetMapping("/{id}")
    public R<TRechargeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tRechargeService.queryById(id));
    }

    /**
     * 新增充值码定义
     */
    @SaCheckPermission("system:recharge:add")
    @Log(title = "充值码定义", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TRechargeBo bo) {
        return toAjax(tRechargeService.insertByBo(bo));
    }

    /**
     * 修改充值码定义
     */
    @SaCheckPermission("system:recharge:edit")
    @Log(title = "充值码定义", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TRechargeBo bo) {
        return toAjax(tRechargeService.updateByBo(bo));
    }

    /**
     * 删除充值码定义
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:recharge:remove")
    @Log(title = "充值码定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tRechargeService.deleteWithValidByIds(List.of(ids), true));
    }
}
